package com.hz.zbl.service.impl;

import com.alibaba.fastjson.JSON;
import com.hz.zbl.client.StudentApi;
import com.hz.zbl.mapper.CourseMapper;
import com.hz.zbl.mapper.UserCourseMapper;
import com.hz.zbl.model.dto.CourseStudentInfoDto;
import com.hz.zbl.model.dto.StudentDTO;
import com.hz.zbl.model.dto.TeacherScheduleDTO;
import com.hz.zbl.model.entity.CourseEntity;
import com.hz.zbl.model.entity.UserCourseEntity;
import com.hz.zbl.model.request.ApiResponse;
import com.hz.zbl.service.TeacherCourseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherCourseService {

    @Resource
    UserCourseMapper userCourseMapper;

    @Resource
    StudentApi studentApi;


    @Resource
    CourseMapper courseMapper;



    @Override
    public int checkScore(int user_score, int student_user_id, String course_code) {
        UserCourseEntity userCourseEntity = userCourseMapper.selectByCourseIdAndStudentId(student_user_id, course_code);
        if (userCourseEntity == null){
            return 0;
        }

        return userCourseMapper.updateUserCourse(user_score, student_user_id, course_code);

    }

    @Override
    public List<TeacherScheduleDTO> teacherSchedule(int teacher_user_id, Integer course_year, Integer course_semester) {
        List<TeacherScheduleDTO> teacherSchedule = new ArrayList<>();

        List<CourseEntity> courseEntities = courseMapper.selectCoursesByTeacherId(teacher_user_id);

        courseEntities.forEach(courseEntity -> {
            TeacherScheduleDTO teacherScheduleDTO = new TeacherScheduleDTO();


            teacherScheduleDTO.setCourse_room(courseEntity.getCourse_room());
            teacherScheduleDTO.setCourse_name(courseEntity.getCourse_name());
            teacherScheduleDTO.setCurrent_section_count(courseEntity.getCurrent_section_count());
            teacherScheduleDTO.setCourse_time(courseEntity.getCourse_time());
            teacherScheduleDTO.setCourse_code(courseEntity.getCourse_code());

            teacherSchedule.add(teacherScheduleDTO);

//            System.out.println(teacherScheduleDTO);
            });
        return teacherSchedule;
    }

    @Override
    public List<CourseStudentInfoDto> teacherCourseStudentInfo(int teacher_user_id, String course_code) {
        List<UserCourseEntity> CourseStudentInfo = userCourseMapper.courseInfo(course_code);
        List<CourseStudentInfoDto> dto = new ArrayList<>();

        CourseStudentInfo.forEach(userCourseEntity -> {

            CourseStudentInfoDto courseStudentInfoDto = new CourseStudentInfoDto();
            courseStudentInfoDto.setStudent_user_id(userCourseEntity.getStudent_user_id());
            courseStudentInfoDto.setStudent_realname(userCourseEntity.getStudent_realname());

            ApiResponse res = com.alibaba.fastjson2.JSON.parseObject(studentApi.studentInfo(userCourseEntity.getStudent_user_id()), ApiResponse.class);
            StudentDTO studentDTO = JSON.parseObject(JSON.toJSONString(res.getData()),StudentDTO.class);

            courseStudentInfoDto.setClass_name(studentDTO.getClass_name());
            courseStudentInfoDto.setGrade_name(studentDTO.getGrade_name());


            dto.add(courseStudentInfoDto);
        });
        return dto;
    }


}
